package com.zzj.ziputil;

import com.zzj.ziputil.ZipProgressUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/* loaded from: classes2.dex */
public class UnZipMainThread extends Thread {
    int lastProgress = 0;
    ZipProgressUtil.ZipListener listener;
    String outPathString;
    String zipFileString;

    public UnZipMainThread(String str, String str2, ZipProgressUtil.ZipListener zipListener) {
        this.zipFileString = str;
        this.outPathString = str2;
        this.listener = zipListener;
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public void run() {
        super.run();
        try {
            this.listener.zipStart();
            long j = 0;
            long zipTrueSize = getZipTrueSize(this.zipFileString);
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.zipFileString));
            while (true) {
                ZipEntry nextEntry = zipInputStream.getNextEntry();
                if (nextEntry != null) {
                    String name = nextEntry.getName();
                    if (nextEntry.isDirectory()) {
                        String substring = name.substring(0, name.length() - 1);
                        new File(this.outPathString + File.separator + substring).mkdirs();
                    } else {
                        File file = new File(this.outPathString + File.separator + name);
                        file.createNewFile();
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        byte[] bArr = new byte[4096];
                        while (true) {
                            int read = zipInputStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            j += read;
                            updateProgress((int) ((100 * j) / zipTrueSize), this.listener);
                            fileOutputStream.write(bArr, 0, read);
                            fileOutputStream.flush();
                        }
                        fileOutputStream.close();
                    }
                } else {
                    this.listener.zipSuccess();
                    zipInputStream.close();
                    return;
                }
            }
        } catch (Exception unused) {
            this.listener.zipFail();
        }
    }

    private void updateProgress(int i, ZipProgressUtil.ZipListener zipListener) {
        if (i > this.lastProgress) {
            this.lastProgress = i;
            zipListener.zipProgress(i);
        }
    }

    public long getZipTrueSize(String str) {
        ZipFile zipFile;
        long j = 0;
        ZipFile zipFile2 = null;
        try {
            try {
                try {
                    zipFile = new ZipFile(str);
                    try {
                        Enumeration<? extends ZipEntry> entries = zipFile.entries();
                        while (entries.hasMoreElements()) {
                            j += entries.nextElement().getSize();
                        }
                        zipFile.close();
                    } catch (IOException e) {
                        e = e;
                        zipFile2 = zipFile;
                        e.printStackTrace();
                        if (zipFile2 != null) {
                            zipFile2.close();
                        }
                        return j;
                    } catch (Throwable th) {
                        th = th;
                        if (zipFile != null) {
                            try {
                                zipFile.close();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        }
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    zipFile = zipFile2;
                }
            } catch (IOException e3) {
                e = e3;
            }
        } catch (IOException e4) {
            e4.printStackTrace();
        }
        return j;
    }
}
